12. Iterating with Each Quiz
Iterating with Each Quiz
Question:
Start Quiz:
<!DOCTYPE html>
<!--
This is the HTML document that you'll use jQuery to modify. To take the quiz, click over to app.js!
-->
<html lang="en">
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<meta charset="UTF-8">
</head>
<body>
<div class="container">
<header class="header">
<nav id="nav" class="navigator">
<h1>Nav Header</h1>
<ul class="nav-list">
<li class="nav-item"><a>Item #1</a></li>
<li class="nav-item active"><a href="#2">Item #2</a></li>
<li class="nav-item"><a href="#3">Item #3</a></li>
<li class="nav-item"><a href="#4">Item #4</a></li>
<li class="nav-item"><a href="#5">Item #5</a></li>
<li class="nav-item"><a href="#6">Item #6</a></li>
</ul>
</nav>
</header>
<div class="articles">
<input id="input" value="Cool Articles" type="text">
<h1>Cool Articles</h1>
<ul class="article-list">
<li class="article-item">
<header>Article #1</header>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Expedita sapiente officiis beatae, ut consequuntur. Quos minus neque eius, nemo sunt excepturi eveniet amet veritatis voluptatibus corporis ea, blanditiis porro ad!</p>
<h3>Sample Image Title here</h3>
<img src="http://placehold.it/350x150" alt="Placeholder Image">
<ul>
<li class="bold">James</li>
<li>Lily</li>
<li>Harry</li>
</ul>
<p>Magnam ex autem doloremque, tempore mollitia atque aut delectus corporis rem similique voluptates omnis reiciendis vitae impedit exercitationem unde quaerat, doloribus voluptatibus molestias et veritatis sed optio repudiandae? Provident, voluptates.</p>
</li>
<li class="article-item featured">
<header>Article #2</header>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil animi ipsum, incidunt mollitia modi cum, eum ex doloremque rerum quod, maiores quisquam, enim quam unde cumque! Quam, doloremque. Voluptatum, maxime!</p>
<p>Numquam et quae, quasi. Reiciendis nemo mollitia eveniet alias, debitis facere atque excepturi et beatae laudantium commodi optio unde amet vitae libero quas cupiditate, nam porro minus. Quisquam, odit non.</p>
</li>
<li class="article-item">
<header>Article #3</header>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Deserunt ratione, cum doloremque molestias labore amet at dicta reiciendis repudiandae fuga atque adipisci pariatur distinctio suscipit perferendis provident, facere ad aut.</p>
<p>Temporibus sapiente odio eveniet enim perferendis maxime, ratione, accusantium suscipit alias, soluta architecto culpa pariatur. Cupiditate nam eaque dolore voluptatem necessitatibus corporis iusto adipisci, dignissimos error, vitae quam, aliquid eius!</p>
</li>
<li class="article-item">
<header>Article #4</header>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate commodi, est soluta eos voluptates eum! Modi sit quia ipsum fugiat nesciunt, assumenda deleniti consequuntur molestias reprehenderit. Voluptate corporis illum nam?</p>
<p>Quia, omnis. Quasi necessitatibus, ducimus, deserunt ipsa, reprehenderit sit a dolore fuga placeat magni culpa ipsam quas id aut veniam rerum pariatur enim harum recusandae quo odio consequuntur. Maxime, aperiam.</p>
</li>
</ul>
</div>
</div>
</body>
</html>
/*
For this quiz, use jQuery's each() method to iterate through the <p>s,
calculate the length of each one, and add each length to the end of each <p>.
Also, make sure you don't change the text inside each <p> except to add the length, otherwise your
length numbers won't be correct!
*/
// Your code goes here!
User's Answer:
(Note: The answer done by the user is not guaranteed to be correct)
<!DOCTYPE html>
<!--
This is the HTML document that you'll use jQuery to modify. To take the quiz, click over to app.js!
-->
<html lang="en">
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<meta charset="UTF-8">
</head>
<body>
<div class="container">
<header class="header">
<nav id="nav" class="navigator">
<h1>Nav Header</h1>
<ul class="nav-list">
<li class="nav-item"><a>Item #1</a></li>
<li class="nav-item active"><a href="#2">Item #2</a></li>
<li class="nav-item"><a href="#3">Item #3</a></li>
<li class="nav-item"><a href="#4">Item #4</a></li>
<li class="nav-item"><a href="#5">Item #5</a></li>
<li class="nav-item"><a href="#6">Item #6</a></li>
</ul>
</nav>
</header>
<div class="articles">
<input id="input" value="Cool Articles" type="text">
<h1>Cool Articles</h1>
<ul class="article-list">
<li class="article-item">
<header>Article #1</header>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Expedita sapiente officiis beatae, ut consequuntur. Quos minus neque eius, nemo sunt excepturi eveniet amet veritatis voluptatibus corporis ea, blanditiis porro ad!</p>
<h3>Sample Image Title here</h3>
<img src="http://placehold.it/350x150" alt="Placeholder Image">
<ul>
<li class="bold">James</li>
<li>Lily</li>
<li>Harry</li>
</ul>
<p>Magnam ex autem doloremque, tempore mollitia atque aut delectus corporis rem similique voluptates omnis reiciendis vitae impedit exercitationem unde quaerat, doloribus voluptatibus molestias et veritatis sed optio repudiandae? Provident, voluptates.</p>
</li>
<li class="article-item featured">
<header>Article #2</header>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil animi ipsum, incidunt mollitia modi cum, eum ex doloremque rerum quod, maiores quisquam, enim quam unde cumque! Quam, doloremque. Voluptatum, maxime!</p>
<p>Numquam et quae, quasi. Reiciendis nemo mollitia eveniet alias, debitis facere atque excepturi et beatae laudantium commodi optio unde amet vitae libero quas cupiditate, nam porro minus. Quisquam, odit non.</p>
</li>
<li class="article-item">
<header>Article #3</header>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Deserunt ratione, cum doloremque molestias labore amet at dicta reiciendis repudiandae fuga atque adipisci pariatur distinctio suscipit perferendis provident, facere ad aut.</p>
<p>Temporibus sapiente odio eveniet enim perferendis maxime, ratione, accusantium suscipit alias, soluta architecto culpa pariatur. Cupiditate nam eaque dolore voluptatem necessitatibus corporis iusto adipisci, dignissimos error, vitae quam, aliquid eius!</p>
</li>
<li class="article-item">
<header>Article #4</header>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate commodi, est soluta eos voluptates eum! Modi sit quia ipsum fugiat nesciunt, assumenda deleniti consequuntur molestias reprehenderit. Voluptate corporis illum nam?</p>
<p>Quia, omnis. Quasi necessitatibus, ducimus, deserunt ipsa, reprehenderit sit a dolore fuga placeat magni culpa ipsam quas id aut veniam rerum pariatur enim harum recusandae quo odio consequuntur. Maxime, aperiam.</p>
</li>
</ul>
</div>
</div>
</body>
</html>
/*
For this quiz, use jQuery's each() method to iterate through the <p>s,
calculate the length of each one, and add each length to the end of each <p>.
Also, make sure you don't change the text inside each <p> except to add the length, otherwise your
length numbers won't be correct!
*/
// Your code goes here!
paragraphs = $(".article-item").find("p");
paragraphs.each(function() {
length = $(this).text().length;
text = $(this).text();
$(this).text(text + " " + length);
})
Solution:
INSTRUCTOR NOTE:
Documentation on .each()
Toggling Between Files
On the Classroom code editor menu, click the down arrow next to index.html
and select app.js
to open the script.